<?php
//set image checker backup (currently uses $loc to turn relative into absolute)
$ImageChecker->setBackupImage($loc . "images/staticbackups/notfound146x106.png");

$Review = new Review();
$Review->getAction($_REQUEST);
$XMLInstanceMoreInfo = new OutputXML('web3g/xml/individuals/' . $_GET['s'] . '.xml');
$prod = $XMLInstanceMoreInfo->AllProductInfo();

// Decode XML-Safe chars
//$badchars  = array("&lt;", "&gt;", "&amp;", "&euro;");
//$goodchars = array("<", ">", "&", "€");

$goodchars = array('<', '>', '€', '&');
$badchars = array('&lt;', '&gt;', '&#8364;', '&amp;');

$prod['Features'] = str_replace($badchars, $goodchars, $prod['Features']);

// Split Features node into two parts: Description and Specifications. 4 x Asterisks is the string separator.
$infoParts = explode("Delivery", $prod['Features']);

//page variables
$product_name = $prod['Name'];
$product_stockcode = $_GET['s'];
$product_category = $_GET['c'];
$product_desc = $infoParts[0];
$delivery_desc = $infoParts[1];
$product_desc_limitnumber = 25;
$product_desc_limit = limitWords($product_desc, $product_desc_limitnumber);
$product_img_loc = "web3g/img/accessories/";
$product_image = "";
$product_thumbnails = Array();
$requested_image = 0; //changed by moreinfoPrep, needed to check when doing thumbnail links so main image is linked correctly
moreinfoPrepImages($product_stockcode); //prep image thumbnails
$product_thumbnails_number = count($product_thumbnails);
$product_image = $ImageChecker->getImg($product_image, "180", "148", $product_name, "", ""); //make sure the product_image is valid
//	$product_image 					= $ImageChecker->getImg($product_image, "146", "106", $product_name, "", ""); //make sure the product_image is valid
//print_r($product_thumbnails);
//echo "<p>$product_image</p>";
//	$product_image 					= $ImageChecker->getImg("/web3g/img/costbiter/". $product_stockcode .".jpg", "146", "106", $product_name, "", "");
//	include('includes/imageSwapper.inc.php');	//SP: Image swapper code, TP Moved it here since product_stockcode is declared a few lines before

if (strlen($prod['Saving']) > 0) {
    $product_saving = "<span>Save " . $prod['Saving'] . "</span>";
} else {
    $product_saving = "<span class=\"costhide\">No Saving</span>";
}
if ($prod['RRP'] > 0) {
    $product_rrp = "<span>was &euro;" . $prod['RRP'] . "</span>";
} else {
    $product_rrp = "<span class=\"costhide\">No RRP</span>";
}
$product_price = $prod['OurPrice'];
$product_features = $infoParts[0];
$product_features_limitnumber = 25;
if($product_features) {
    $product_features_limit = limitWords($product_features, $product_features_limitnumber);
//	$product_features_limited		= truncateString($product_features, "<BR />", 3);
    $product_features_limited = substr($product_features, 0, strpos(str_replace("<BR />", "<BR*/>", $product_features), " ", 90)) . "...";
}
if ($product_features_limited == "...")
    $product_features_limited = "";

$currentPage = explode("/", $_SERVER["REQUEST_URI"]);
//var_dump($currentPage);
//echo $_REQUEST['img'];
switch ($_GET['ext']) { //grab the current menu if requested
    case "prodAccessories": $product_current_menu = "prodAccessories";
        break;
    case "prodSpec": $product_current_menu = "prodSpec";
        break;
    case "prodReviews": $product_current_menu = "prodReviews";
        break;
    case "prodDelivery": $product_current_menu = "prodDelivery";
        break;
    default: $product_current_menu = "prodAccessories";
        break;
}
?>
<div id="moreinfocontainer">
    <div id="breadcrumb" style="padding:8px 0px 8px 0px;">
        <?php
        $tpl_friendly = utf8_encode($CleanCode->Encode($prod['tpl']));
        $mcl_friendly = utf8_encode($CleanCode->Encode($prod['mcl']));
        $scl_friendly = utf8_encode($CleanCode->Encode($prod['scl']));
        echo " | <a href=\"" . $loc . "\"><b>Home</b></a> | <a href=\"" . $loc . "search/" . $tpl_friendly . "/\"><b>" . $prod['tpl'] . "</b></a>";
        echo " | <a href=\"" . $loc . "search/" . $tpl_friendly . "/" . $mcl_friendly . "/\">" . $prod['mcl'] . "</a>";
        echo " | <a href=\"" . $loc . "search/" . $tpl_friendly . "/" . $mcl_friendly . "/" . $scl_friendly . "/\">" . $prod['scl'] . "</a>";
        //echo " | <a href=\"" . $loc . "moreinfo/" . $prod['Stockcode'] . "/" . $tpl_friendly . "/\">" . $prod['Name'] . "</a>";
        echo " | " . $prod['Name'];
        ?>
    </div>
    <div id="refineSearch">
        <?php include('includes/searchOptions.inc.php'); ?>
    </div>
    <div id="productInfo">
        <div id="productDesc">
            <h3><?php echo $product_name; ?></h3>
            <div id="productDescLimit"></div>
            <noscript>
                <span>
                    <?php
                    if ($_GET['fulldesc']) {
                        echo str_replace("<BR />", "", $product_desc);
                    } else {
                        echo $product_desc_limit . '...<a href="/moreinfo/' . $product_stockcode . '/' . $product_category . '/&fulldesc=true">Mehr Info</a>';
                    }
                    ?>
                </span>
            </noscript>
            <script type="text/javascript">
                if(document.getElementById('productDescLimit')){
                    document.getElementById('productDescLimit').innerHTML=moreinfo.limitWords("<?php echo htmlspecialchars(str_replace("<BR />", "", $product_desc)); ?>",<?php echo $product_desc_limitnumber; ?>);
                }
            </script>
        </div>
        <div class="spacer"></div>
        <div id="productDetail">
            <div id="imgBox">
                <?php
                //show main image
                echo "\t\t\t<div id=\"prodMainImageContainer\">\n";
                echo "\t\t\t\t" . $product_image . "\n";
                echo "\t\t\t</div>\n";

                //show thumbnails
                echo "\t\t\t<div id=\"prodThumbnailContainer\">\n";
                for ($i = 0; $i < $product_thumbnails_number; $i++) {
                    $current_index = $product_thumbnails[$i]['index'];
                    $current_thumbnail = $ImageChecker->getImg($product_thumbnails[$i]['loc'], "52", "42", $product_name . " thumbnail $current_index", "", "");
                    echo "\t\t\t\t<div class=\"prodThumbnail\">\n";
                    if ($i == 0 && $requested_image > 0) { //if true then the primary product image is currently a thumbnail, so a tweaking of the url is needed
                        echo "\t\t\t\t\t<a href=\"" . $loc . "moreinfo/" . $product_stockcode . "/" . $product_category . "/" . $product_current_menu . "\">" . $current_thumbnail . "</a>\n";
                    } else {
                        echo "\t\t\t\t\t<a href=\"" . $loc . "moreinfo/" . $product_stockcode . "/" . $product_category . "/" . $product_current_menu . "/image-" . $current_index . "\">" . $current_thumbnail . "</a>\n";
                    }
                    echo "\t\t\t\t</div>\n";
                }
                if ($product_thumbnails_number > 1) {
                    echo "\t\t\t\t<p>Click image to enlarge</p>\n";
                }
                echo "\t\t\t</div>\n";
                ?>
            </div>
            <?php
            //count the number of rows of thumbs so a class can be inserted into detailBox to increase its size
            if ($product_thumbnails_number > 1) {
                if ($product_thumbnails_number > 6) {
                    $detailClass = " class=\"rowsthree\"";
                } else if ($product_thumbnails_number > 3) {
                    $detailClass = " class=\"rowstwo\"";
                } else {
                    $detailClass = " class=\"rowsone\"";
                }
            } else {
                $detailClass = "";
            }
            ?>
            <div id="detailBox"<?php echo $detailClass; ?>>
                <div id="detailBoxInner">
                    <div id="detailBox_top">
                        <div id="detailBox_topprice">
                            <?php /* ?>	<p class="rrp"><?php echo $product_rrp; ?></p><?php */ ?>
                            <br /><p class="price">&euro;<?php echo number_format($product_price, 2); ?></p>
                            <?php /* ?><p class="saving"><?php echo $product_saving; ?></p><?php */ ?>
                        </div>
                        <div id="detailBox_top_buy">
                            <form action="/basket/" method="post" style="display:inline-block;">
                                <input type="hidden" name="esid" value="<?php echo $_SESSION['esid']; ?>" />
                                <input type="hidden" name="acid" value="<?php echo $prod['StockCode'] ?>" />
                                <input type="image" src="/images/spacer.gif" class="spritebg bigbuyBtn" />
                            </form>
                        </div>
                    </div>
                    <div id="detailBox_features">
                        <h2>Produkt Eigenschaften</h2>
                        <div id="detailBox_features_info">
                            <?php echo $product_features_limited; ?><br />
                            <a href="/moreinfo/<?php echo $product_stockcode; ?>/<?php echo $product_category; ?>/prodSpec#menu"><strong>Alle Details hier...</strong></a>
                        </div>
                    </div>
                    <div id="option_box">
                        <!--<div id="wishlist"><a href="#">Add to wishlist</a></div>-->
                        <?php /* ?><div id="delivery"><a href="#"><strong>FREE DELIVERY</strong></a></div><?php */ ?>
                    </div>
                </div>
            </div>
            <img id="productDetailShadow" src="/images/productzone/prod_btmshad.gif" />
        </div>
        <div id="productViewBasketButton"><a href="/basket/" style="width:132px; height:32px;"><img src="/images/spacer.gif" width="132" height="32" alt="" /></a></div>
        <a name="menu"></a>
        <div id="productExtras">
            <?php
            $prodAccessories = "hide";
            $prodSpec = "hide";
            $prodReviews = "hide";
            $prodDelivery = "hide";
            $prodbg = "/images/tabbedsection/info_tabbed_tab_acc.png";
            $prodmenucssclass = "accessories";
            switch ($_GET['ext']) {
                /*
                              case "prodAccessories": $prodAccessories = "show"; 	$prodbg = "/images/tabbedsection/info_tabbed_tab_acc.png"; $prodmenucssclass = "prodExtraMenuaccessories"; break; */
                case "prodSpec": $prodSpec = "show";
                    $prodbg = "/images/tabbedsection/info_tabbed_tab_acc.png";
                    $prodmenucssclass = "prodExtraMenuspecification";
                    break;
                /*
                              case "prodReviews":$prodReviews = "show"; 	$prodbg = "/images/tabbedsection/info_tabbed_tab_rev.png"; $prodmenucssclass = "prodExtraMenureviews"; break; */
                case "prodDelivery":$prodDelivery = "show";
                    $prodbg = "/images/tabbedsection/info_tabbed_tab_spec.png";
                    $prodmenucssclass = "prodExtraMenudelivery";
                    break;
                default: $prodSpec = "show";
                    $prodbg = "/images/tabbedsection/info_tabbed_tab_acc.png";
                    $prodmenucssclass = "prodExtraMenuspecification";
                    break;
            }
            ?>
            <div class="prodExtraMenu" id="<?php echo $prodmenucssclass; ?>">
                <a href="/moreinfo/<?php echo $product_stockcode; ?>/<?php echo $product_category; ?>/prodSpec#menu"><img src="/images/spacer.gif" width="130" height="32" /></a>
                <?php /* ?><a href="/moreinfo/<?php echo $product_stockcode; ?>/<?php echo $product_category; ?>/prodAccessories#menu"><img src="/images/spacer.gif" width="130" height="32" /></a>

                          <a href="/moreinfo/<?php echo $product_stockcode; ?>/<?php echo $product_category; ?>/prodReviews#menu"><img src="/images/spacer.gif" width="130" height="32" /></a><?php */ ?>
                <a href="/moreinfo/<?php echo $product_stockcode; ?>/<?php echo $product_category; ?>/prodDelivery#menu"><img src="/images/spacer.gif" width="130" height="32" /></a>
            </div>
            <div class="prodExtraCon">
                <div id="prodAccessories" class="prodExtraBox <?php echo $prodAccessories; ?>">
                    <div class="prodExtraBoxInner">
                        <?php echo Language::Get('moreinfo', 5); ?>
                    </div>
                </div>
                <div id="prodSpec" class="prodExtraBox <?php echo $prodSpec; ?>">
                    <?php /* ?><div class="spec">
                          <div class="specTitle">title spec1</div> <div class="specValue">value</div>
                          </div>
                          <div class="spec">
                          <div class="specTitle">title spec1</div> <div class="specValue">value</div>
                          </div>
                          <div class="spec">
                          <div class="specTitle">title spec1</div> <div class="specValue">value</div>
                          </div><?php */ ?>
                    <div class="prodExtraBoxInner">
                        <?php echo $product_features; ?>
                    </div>
                </div>
                <div id="prodReviews" class="prodExtraBox <?php echo $prodReviews; ?>">
                    <div class="prodExtraBoxInner">
                        <?php echo Language::Get('moreinfo', 5); ?>
                    </div>
                    <?php /* ?><div class="reviewEnter">
                          <?php if(Login::CheckStatus()){ ?>
                          <?php require_once('includes/reviewForm.inc.php'); ?>
                          <?php }else{ ?>
                          <div style="text-align:center;"><?php echo Language::Get('moreinfo',0); ?></div>
                          <?php } ?>
                          </div>
                          <?php $sql = "SELECT * FROM CostBiterReviews R INNER JOIN CostBiterUsers U ON R.User=U.EMail WHERE R.StockCode = ".$_GET['s']." AND R.Valid=0";
                          $result = DB::query($sql);
                          while($fetch = DB::fetch($result)){ ?>
                          <div class="review">
                          <div class="reviewUser"><span><?php echo Language::Get('moreinfo',1); ?></span> <?php echo $fetch['Firstname']; ?> <?php echo substr($fetch['Surname'],0,1); ?></div><div class="ratingRate"><span><?php echo Language::Get('moreinfo',2); ?></span>
                          <?php
                          if($fetch['Rating'] > 0){
                          $c=0;
                          while($c < $fetch['Rating']){
                          echo '<div class="colorstar"></div>';
                          $c++;
                          }
                          $c=0;
                          $greystar = 5-$fetch['Rating'];
                          while($c < $greystar){
                          echo '<div class="greystar"></div>';
                          $c++;
                          }
                          }else{
                          echo Language::Get('moreinfo',4);
                          }
                          ?>
                          </div>
                          <div class="reviewLine"></div>
                          <?php echo $fetch['Review']; ?>
                          </div>
                          <?php $i++; } ?><?php */ ?>
                </div>
                <div id="prodDelivery" class="prodExtraBox <?php echo $prodDelivery; ?>">
                    <div class="prodExtraBoxInner">
                        <?php echo $delivery_desc; ?>
                    </div>
                </div>
            </div>
            <img id="prodExtraShadow" src="/images/tabbedsection/info_tabbed_btmshad.gif" />
        </div>
    </div>
</div>
<?php

function limitWords($string, $word_limit) {
    return implode(" ", array_splice(explode(" ", $string), 0, $word_limit));
}

function truncateString($string, $needle, $needle_limit) { //SP + TP
    //if string or needle is empty, bomb out returning the original string
    if (strlen($string) < 1 || strlen($needle)) {
        return $string;
    }

    // Function to truncate $string after $needle_limit instances of $needle (SP)
    $needle_length = strlen($needle);
    $needle_count = 0;
    $offset = 0;

    while ($needle_count < $needle_limit) {
        if ($offset = strpos($string, $needle, $offset)) {
            $needle_count++;
            if ($needle_count < $needle_limit) {
                // We do not want to add X chars (where X = $needle_length) on last iteration of loop
                // Only do this if needle_count less than $needle_limit
                $offset += $needle_length;
            } else {
                $needle_count++;
            }
        }
    }
    $trim = substr($string, 0, $offset);
    return $trim;
}

function moreinfoPrepImages($stockcode) { //TP - autodetect additional images and main image
    GLOBAL $product_image;
    GLOBAL $product_thumbnails;
    GLOBAL $product_img_loc;
    GLOBAL $requested_image;
    $image_prefix = "lg";
    $product_thumbnails_index = 0;

    //check that requested image is available and valid (otherwise set requested to 0 as default)
    if (isset($_REQUEST['img']) && is_numeric($_REQUEST['img'])) {
        $requested_image = $_REQUEST['img'];
        //check that the image exists
        $current_img_loc = $_SERVER['DOCUMENT_ROOT'] . "/" . $product_img_loc . $image_prefix . $stockcode . "_0" . $requested_image . ".jpg";
        if (!file_exists($current_img_loc)) {
            $requested_image = 0;
        } //image not found, the set requested image to main (0)
    }

    //set the main image
    if ($requested_image == 0) {
        $product_image = $product_img_loc . $image_prefix . $stockcode . ".jpg";
    } else {
        $product_image = $product_img_loc . $image_prefix . $stockcode . "_0" . $requested_image . ".jpg";
    }

    //try and sequentially detect the images and put into the thumbnails array
    for ($i = 0; $i < 10; $i++) { //do a for loop and breakout when needed, limit to 10 anyway. starting from 0 incase main image is first thumbnail
        if ($i == 0 && $requested_image != 0) { //if another image has been requested, then show the main image as a thumbnail
            $product_thumbnails[$product_thumbnails_index]['loc'] = $product_img_loc . $stockcode . ".jpg";  //store relative link
            $product_thumbnails[$product_thumbnails_index]['index'] = 0;  //store index
            $product_thumbnails_index++;
        } else {
            if ($i == 0) {
                $i++;
            } //skip zero because our thumbnail images start from 1
            if ($i != $requested_image) { //only process thumbnails that haven't been requested
                $current_img_loc = $_SERVER['DOCUMENT_ROOT'] . "/" . $product_img_loc . $stockcode . "_0" . $i . ".jpg";
                if (file_exists($current_img_loc)) {
                    $product_thumbnails[$product_thumbnails_index]['loc'] = $product_img_loc . $stockcode . "_0" . $i . ".jpg"; //store relative link
                    $product_thumbnails[$product_thumbnails_index]['index'] = $i;  //store index
                    $product_thumbnails_index++;
                } else {
                    $i = 10;
                } //break the loop since the sequence has been broken
            }
        }
    }
}
?>